$(function(){

    //表单验证
    $("#registerForm").validate({
        rules:{
            companyName:{
                required:true,
                companyName:true
            },
            loginName:{
                required:true,
                loginName:true,
                loginNameIsExists:true
            },
            password:{
                required:true,
                password:true
            },
            passwordEnsure:{
                required:true,
                equalTo:"#password"
            },
            validCode:{
                required:true
            }
        },
        messages:{
            companyName:{
                required:"请输入企业名称"
            },
            loginName:{
                required:"请输入登陆名称"
            },
            password:{
                required:"请输入登陆密码"
            },
            passwordEnsure:{
                required:"请再次输入登陆密码",
                equalTo:"两次所输入的密码不一致"
            },
            validCode:{
                required:"请输入验证码"
            }
        },
        onkeyup:false
    });

    if (top.location != self.location){
        top.location = self.location;
    }

    $("#companyName").focus(function(){
        if ($("#companyName").val() == "请输入企业名称"){
            $("#companyName").val("").addClass("en_word");
        }
    });

    $("#companyName").blur(function(){
        if($("#companyName").val() == "请输入企业名称" || $(this).val() == ""){
            $("#companyName").val("请输入企业名称").addClass("out_word");
        }
    });

    $("#loginName").focus(function(){
        if ($("#loginName").val() == "请输入登录名"){
            $("#loginName").val("").addClass("en_word");
        }
    });

    $("#loginName").blur(function(){
        if($("#loginName").val() == "请输入登录名" || $(this).val() == ""){
            $("#loginName").val("请输入登录名").addClass("out_word");
        }
    });

    $("#password2").focus(function(){
        if ($("#password2").val() == "请输入密码"){
            $("#password").val("").attr("style", "display:inline-block").focus();
            $("#password2").attr("style", "display:none");
        }
    });

    $("#password").blur(function(){
        if($("#password").val() == "请输入密码" || $("#password").val() == ""){
            $("#password2").attr("style", "display:inline-block");
            $("#password2").val("请输入密码");
            $("#password2").addClass("out_word");
            $("#password").attr("style", "display:none");
        }
    });

    $("#passwordEnsure2").focus(function(){
        if ($("#passwordEnsure2").val() == "请再次输入密码"){
            $("#passwordEnsure").val("").attr("style", "display:inline-block").focus();
            $("#passwordEnsure2").attr("style", "display:none");
        }
    });

    $("#passwordEnsure").blur(function(){
        if($("#passwordEnsure").val() == "请再次输入密码" || $("#password").val() == ""){
            $("#passwordEnsure2").attr("style", "display:inline-block");
            $("#passwordEnsure2").val("请再次输入密码");
            $("#passwordEnsure2").addClass("out_word");
            $("#passwordEnsure").attr("style", "display:none");
        }
    });


    $("#validCode").focus(function(){
        if ($("#validCode").val() == "请输入验证码"){
            $("#validCode").css("border-color", "#CFCFCF");
            $("#validCode").val("").addClass("en_word");
        }
    });

    $("#validCode").blur(function(){
        if($("#validCode").val() == "请输入验证码" || $(this).val() == ""){
            $("#validCode").val("请输入验证码");
            $("#validCode").addClass("out_word");
        }
    });

    //回车事件，注册
    $(document).keyup(function (event) {
        var keycode = event.which;
        if (keycode == 13) {
            register();
        }
    });

    //获取企业类型数据，填入下拉框
    $.ajax({
        url: CONTEXT_PATH +'/commonDict/getCommonDictOptions.do?modelCode=PR&funCode=unitProperty&propertyParentCode=0',
        type: "get",
        success: function(data){
            if(data && data != ""){
                for(index in data){
                    var option = '<option value="'+ data[index].cdId+ '">' + data[index].propertyName + '</option>>'
                    $("#companyType").append(option);
                }
            }
        }
    });

});

//注册事件
function register(){
    var params = $("#registerForm").serialize();
    if(!$("#registerForm").valid()){
        return false;
    }
    submit(params);
}

//注册提交
function submit(params){
    $.ajax({
        type:'post',
        url: CONTEXT_PATH + "/userLogin/register.do",
        data: params,
        success:function(data){
            //延时1秒执行
            setTimeout( function(){
                //注册成功
                if(data.code == 'PC_200'){
                    $(".forMask").mask("注册成功，5秒后跳转至登陆页面");
                    setTimeout(function(){
                        window.location.href = CONTEXT_PATH + "/";
                    }, 5000)

                }else{
                    $(".forMask").mask(data.message);
                    setTimeout(function(){reset_btn()},2000);
                }
            },1000);
        },
        beforeSend: function(){
            $(".forMask").mask("用户注册处理中，请稍候…");
        }
    });
}

//重置
function reset_btn() {
    window.location.reload();
}